﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    RecommendAward
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 15:01:34
// Update Time         :    2012/6/24 15:01:34
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Account;
using Gfw.BLL.Basic.ExceptionImpl;
using Gfw.BLL.Basic.User;
using Gfw.Models.Account;
using Gfw.Models.Admin;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 直推奖
    /// </summary>
    public class RecommendAward : IFacility
    {
        private double amount;
        private string recommendUserId;

        /// <summary>
        /// 直推将，将从公司帐户（奖金预提）转入用户帐户
        /// </summary>
        /// <param name="userAmount">直接推奖获奖用户帐号</param>
        /// <param name="amount">奖金金额</param>
        public RecommendAward(string recommendUserId, double amount)
        {
            this.recommendUserId = recommendUserId;
            this.amount = amount;
        }

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();

            string awardSerialNo = Guid.NewGuid().ToString();

            //公司帐户取出amount金额
            ActCompanyBargainRepository companyBargain = new ActCompanyBargainRepository();
            companyBargain.AddOutlay(recommendUserId, (int) CompanyTradeTypeEnum.Recommend, amount, "", awardSerialNo,
                                     work);



            //用户上税
            AwardTax at = new AwardTax(recommendUserId, amount, awardSerialNo, null);
            at.Execute(work);

            //奖金帐户分配
            AwardAllot aa = new AwardAllot(recommendUserId, at.IndividualAward(), awardSerialNo, null,
                                           UserTradeTypeEnum.Recommend);
            aa.Execute(work);

            //个人用户的奖金
            double individual = aa.IndividualAward();
            double tax = amount - individual;
            //插入动态奖金分配表
            ActUserDynamicawardRepository userAwardRepository = new ActUserDynamicawardRepository();
            userAwardRepository.AddAward(recommendUserId, (int) UserTradeTypeEnum.Recommend, amount, individual, tax,
                                         awardSerialNo, work);
            if (unitOfWork == null)
                work.Commit();
        }
    }
}
